package com.sure.yyr.security.user.impl;

import com.sure.yyr.common.Utils;
import com.sure.yyr.common.page.QueryAction;
import com.sure.yyr.security.user.UserDto;
import com.sure.yyr.security.user.UserService;
import com.sure.yyr.security.user.User;
import com.sure.yyr.security.user.dao.UserDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service //说明这是一个受spring容器管理的业务组件(Bean)
@Transactional //说明本类的所有方法都是事务性
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;




    @Override
    public Map<String, Object> getUserPage(UserDto dto) {
        //Map<String,Object> page = Utils.getPage(dto, ()-> userDao.findUserListByCondition(dto));
//
//        //内部类
//        class MyAction implements QueryAction<User>{
//
//            @Override
//            public List<User> execute() {
//                return userDao.findUserListByCondition(dto);
//            }
//        }
//
//
//        Map<String, Object> page =Utils.getPage(dto, new MyAction());
//

//        Map<String, Object> page =Utils.getPage(dto, new QueryAction<User>(){
//
//            @Override
//            public List<User> execute() {
//                return userDao.findUserListByCondition(dto);
//            }
//        });

        //jdk1.8，引入了lambda表达式，对于仅有一个方法的接口可以简写
//        Map<String, Object> page =Utils.getPage(dto, ()->{
//
//
//                return userDao.findUserListByCondition(dto);
//
//        });

        //对于仅有一条语句，并且直接返回结果，可以进一步简化
        Map<String, Object> page =Utils.getPage(dto, ()->userDao.findUserListByCondition(dto));



        return page;
    }

    @Override
    public void addUser(UserDto dto) {
        User user = getUserById(dto.getU_id());
        if(user == null){
            throw new RuntimeException("已存在相同账号的用户！");
        }
        dto.setU_status("00");
        dto.setU_pwd(DEFAULT_PASSWORD);
        userDao.insertUser(dto);
    }

    @Override
    public void updateUser(UserDto dto) {
        userDao.updateUser(dto);
    }

    @Override
    public void deleteUser(String... u_ids) {
        if(u_ids==null || u_ids.length ==0){
            throw new RuntimeException("没有获得需要删除用户的账号！");
        }
        //按照账号查询用户的角色数量
        int count = userDao.findRoleCountByUserIds(u_ids);
        if(count>0){
            throw new RuntimeException("部分用户已拥有角色，不允许删除！");
        }
        userDao.deleteUser(u_ids);
    }

    @Override
    public User getUserById(String u_id) {
        User user = userDao.findUserById(u_id);
        return user;
    }

    @Override
    public void enableUser(String u_id) {
        User user = userDao.findUserById(u_id);
        if(user.getU_status().equals("55")){
            throw new RuntimeException("用户状态已经为启用！");
        }
        UserDto dto = new UserDto();
        dto.setU_id(u_id);
        dto.setU_status("55");
        userDao.updateUser(dto);
    }

    @Override
    public void disableUser(String u_id) {
        User user = userDao.findUserById(u_id);
        if(!user.getU_status().equals("55")){
            throw new RuntimeException("只能禁用已启用的用户！");
        }
        UserDto dto = new UserDto();
        dto.setU_id(u_id);
        dto.setU_status("77");
        userDao.updateUser(dto);
    }
}
